﻿using StreamServeDSIGenerator.Entities;
using StreamServeDSIGenerator.UI.Process.PromptDialog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StreamServeDSIGenerator.UI.Process
{
    public class importDialogControl
    {
        public Form mainForm { set; get; }
        public ListBox templateListBox { set; get; }
        public TextBox txtTemplate { set; get; }

        private List<DataTemplate>DataTemplateList = new List<DataTemplate>();


        private void childDialog_validate(object sender, CancelEventArgs e)
        {
            importDialog childDialog = sender as importDialog;

            string fileName = String.Empty;
            string separator = String.Empty;

            fileName = childDialog.txtFileName.Text.Trim();
            separator = childDialog.txtBlockStart.Text.Trim();

            if (String.IsNullOrEmpty(fileName))
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }



        }
        public void OpenDialog()
        {
            importDialog childDialog = new importDialog();
            childDialog.validate += childDialog_validate;
        
            //ImportChildDialog.validate += childDialog_validate;
            if (childDialog.ShowDialog(mainForm) == System.Windows.Forms.DialogResult.OK)
            {


                Stream stream = null;
                string line = String.Empty;
                string blockStart = String.Empty;
                string fileName = String.Empty;

                blockStart = childDialog.txtBlockStart.Text.Trim();
                fileName = childDialog.txtFileName.Text.Trim();
                bool isFirstLine = true;
                DataTemplate importChunkDataTemplate = new DataTemplate();
                txtTemplate.Text = "";



                try
                {
                    if ((stream = File.OpenRead(fileName)) != null)
                    {
                        DataTemplateList.Clear();


                        using (StreamReader file = new StreamReader(stream))
                        {
                            while ((line = file.ReadLine()) != null)
                            {
                                if (!isFirstLine && line.StartsWith(blockStart, StringComparison.Ordinal))
                                {
                                    DataTemplateList.Add(importChunkDataTemplate);
                                    importChunkDataTemplate = new DataTemplate();
                                }

                                importChunkDataTemplate.DataTemplateText += line + Environment.NewLine;
                                isFirstLine = false;

                                //    if(file.rea
                            }
                            if (!String.IsNullOrEmpty(importChunkDataTemplate.DataTemplateText))
                            {
                                DataTemplateList.Add(importChunkDataTemplate);
                                importChunkDataTemplate = null;
                            }
                            //while (file.Peek()>=0)
                            //{
                            //    using(line = file.rea
                            //}
                            //    file.rea
                            //    //while ((line = file.ReadLines().Last()) != null)
                            //    //{
                            //    //    //if(file.l
                            //    //    //if (!isFirstLine && line.StartsWith("0#", StringComparison.Ordinal))
                            //    //    //{
                            //    //    //    DataTemplateList.Add(importChunkDataTemplate);
                            //    //    //    importChunkDataTemplate = new DataTemplate();
                            //    //    //}


                            //    //    //


                            //    //}
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to open selected template text file.", "Cannot Open File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

          //          repopulateListItem();
                

              
            }


        }



    }
}
